package com.baiyinet.util.webview;

import android.content.Context;
import android.os.AsyncTask;
import android.os.SystemClock;
import android.webkit.WebView;

import com.baiyinet.util.CookieHelper;

import java.util.Map;
import java.util.WeakHashMap;

/**
 * @author J.Huang
 * @version 1.0
 * @since 17/1/23
 */

public class WebViewTask extends AsyncTask<Void, Void, Boolean> {
    private Context mContext;
    private WebView mWebView;
    private String mRequestUrl;
    private String mCookieInfo;

    public WebViewTask(Context context, WebView webView, String requestUrl, String cookieInfo) {
        mContext = context;
        mWebView = webView;
        mRequestUrl = requestUrl;
        mCookieInfo = cookieInfo;
    }

    @Override
    protected void onPreExecute() {
        CookieHelper.setWebViewRequestUrlCookie(mContext, mRequestUrl, mCookieInfo);

        super.onPreExecute();
    }
    protected Boolean doInBackground(Void... param) {
        /* this is very important - THIS IS THE HACK */
        SystemClock.sleep(1000);
        return false;
    }
    @Override
    protected void onPostExecute(Boolean result) {
        Map<String, String> cookies = new WeakHashMap<>();
        cookies.put("AppOS", "Android");

        mWebView.clearView();
        mWebView.loadUrl(mRequestUrl, cookies);
    }
}
